//
//  ZZFTimeKeeperBtn.m
//  ZZFTimeKeeperBtn
//
//  Created by 瞿衡 on 17/1/10.
//  Copyright © 2016年 瞿衡. All rights reserved.
//

#import "BZBaseTimeKeeperBtn.h"
#import <objc/runtime.h>

@interface BZBaseTimeKeeperBtn()
/**
 *  控件的状态
 */
@property(nonatomic, assign) BZStatus  statusType;
/**
 *  计时的时间
 */
@property(nonatomic, assign) NSInteger  timeCount;
/**
 *  计时原始时间
 */
@property(nonatomic, assign) NSInteger  oldTimeCount;
/**
 *  计时器
 */
@property(nonatomic, weak  ) NSTimer    *timer;
/**
 *  按钮的初始背景色
 */
@property(nonatomic, strong) UIColor    *normalBgColor;
/**
 *  按钮的初始背景图片
 */
@property(nonatomic, strong) UIImage    *normalBgImg;
/**
 *  按钮默认情况下的文字颜色
 */
@property(nonatomic, strong) UIColor   *normalTextColor;

@property(nonatomic, copy  ) NSString  *normalText;

@end
//用于关联block
static void *statusBlockKey = @"statusBlockKey";

@implementation BZBaseTimeKeeperBtn

//重写buttonWithType 方法 UIButtonTypeCustom按钮计时改变的时候不会闪动
+ (instancetype)buttonWithType:(UIButtonType)buttonType{
    
    return [super buttonWithType:UIButtonTypeCustom];
}

- (void)setDGTimerButtonWithDuration:(NSInteger)durtaion
                            buStatus:(void(^)(BZStatus status))bustatus{
    
    _timeCount      = durtaion;
    _oldTimeCount   = durtaion;
    _statusType     = BUStatusNone;
    
    objc_setAssociatedObject(self, statusBlockKey, bustatus, OBJC_ASSOCIATION_COPY);
    
    bustatus(BUStatusNone);
}
- (void)setDGTimerButtonWithDuration:(NSInteger)durtaion
                         runingColor:(UIColor *)runingColor
                     runingTextColor:(UIColor *)runingTextColor
                       runingImgName:(NSString *)runingImgName
                           formatStr:(NSString *)formatStr
                            buStatus:(void(^)(BZStatus status))bustatus{
    
    _runingImgName      =runingImgName;
    _runingColor        =runingColor;
    _runingTextColor    =runingTextColor;
    _formatStr          =formatStr;
    [self setDGTimerButtonWithDuration:durtaion buStatus:^(BZStatus status) {
        bustatus(status);
    }];
}


- (void)beginTimers{
    
  if (!self.timer) {
    NSTimer *timer = [NSTimer timerWithTimeInterval:1
                                             target:self
                                           selector:@selector(next)
                                           userInfo:nil
                                            repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
  }
    //更新时间
    _timeCount           = _oldTimeCount;
    //记录按钮的原始状态
    _normalBgColor       = self.backgroundColor;
    _normalBgImg         = self.currentBackgroundImage;
    _normalTextColor     = self.currentTitleColor;
    _normalText          = self.currentTitle;
    //设置按钮计时时的样式
    if (_runingColor) {
        [self setBackgroundColor:_runingColor];
    }
    if (_runingImgName.length > 0) {
        [self setBackgroundImage:[UIImage imageNamed:_runingImgName] forState:UIControlStateNormal];
    }
    if (_runingTextColor) {
        [self setTitleColor:_runingTextColor forState:UIControlStateNormal];
    }
    //让按钮不可以点击
    self.userInteractionEnabled = NO;
    self.titleLabel.adjustsFontSizeToFitWidth=YES;
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self next];
    
    void (^statusBlock)(NSInteger) = objc_getAssociatedObject(self, statusBlockKey);
    statusBlock(BUStatusRuning);

}

- (void)stopTimers{
    
    [self.timer invalidate];
     self.timer = nil;
    void (^statusBlock)(NSInteger) = objc_getAssociatedObject(self, statusBlockKey);

    if (_timeCount ==0) {//超时了
        statusBlock(BUStatusFinish);
    }else{
        //结束了但没有超时
        statusBlock(BUStatusCancel);
    }
    //还原按钮样式
     [self setBackgroundImage:_normalBgImg forState:UIControlStateNormal];
     [self setBackgroundColor:_normalBgColor];
     [self setTitleColor:_normalTextColor forState:UIControlStateNormal];
     [self setTitle:_normalText forState:UIControlStateNormal];
      self.userInteractionEnabled = YES;
}

- (void)next{
    
    if (_timeCount<1) {
        //结束了 超时了
        [self stopTimers];
    }else{
         //改文字
        if (_formatStr.length>0) {
           [self setTitle:[NSString stringWithFormat:_formatStr,_timeCount] forState:UIControlStateNormal];
        }else{
            [self setTitle:[NSString stringWithFormat:@"%zdS",_timeCount] forState:UIControlStateNormal];
        }
        _timeCount--;
    }
}


@end
